package com.qf.security1.web;

import com.qf.security1.pojo.R;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;

@RestController
@RequestMapping("/user")
public class UserController {

    @GetMapping("/test1")
    public R test1(){

        //获取当前登录的用户信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        if (authentication != null){
            String username = (String) authentication.getPrincipal();
            System.out.println(username);

            Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
            authorities.forEach(System.out::println);
        }

        return R.success("security test1...");
    }
}
